#Copyright (C) 2009 izzyrider, <izzyrider@gmail.com>
# Version 0.4
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US

__module_name__ = "xrbox"
__module_description__ = "Controls Rhythmbox from within XChat (!rhelp for help)."
__module_version__ = "0.4"
import xchat, commands, os, fnmatch

# CHANGE THE DATA FOR YOUR CONFIGURATION HERE :
# The name of the channel where you want messages to appear / be triggered, for instance
CHANNEL = '#hipzone-live'

# No need to change this, unless using a server where the symbols have other meanings
# + - voice, @ - operator, ~ - owner
# Need to be op or owner to work with all commands, except !rshow, only voice required.
IRC_SYMBOLS = ['+','@','~']

# Choose colors for the elements that appear in the chat
# By default, they have the default color. To change it, edit the following entries,
# changing the numbers to a number of the following list:
#  2 - Blue (Navy)       3 - Green              4 - Red         5 - Brown (Maroon)
#  6 - Purple            7 - Orange (Olive)     8 - Yellow      9 - Light green (Lime)
# 10 - Teal (Blue Cyan) 11 - Light Cyan (Aqua) 12 - Light Blue 13 - Pink  
# 14 - Gray             15 - Default (White)

COLOR_ARTIST = 15
COLOR_SONG = 15
COLOR_DURATION = 15
COLOR_MESSAGES= 15

def message_cb(word, word_eol, userdata):
    global CHANNEL, IRC_SYMBOLS
    global COLOR_ARTIST, COLOR_SONG, COLOR_DURATION, COLOR_MESSAGES
    mychannel = xchat.get_info('channel')
    voiced = (len(word) >= 3) and (word[2] in IRC_SYMBOLS)
    opped = False
    if voiced:
      opped = (word[2] in IRC_SYMBOLS[1:])
    if (mychannel != CHANNEL or (not voiced)):
      return xchat.EAT_NONE
    message=word[1]
    if message == "!rshow":
      song = chr(2) + chr(3) + str(COLOR_SONG)
      song += commands.getoutput("rhythmbox-client --print-playing-format \'%tt\'")
      song += chr(3) + str(15) + ' - ' + chr(3) + str(COLOR_ARTIST)
      song += commands.getoutput("rhythmbox-client --print-playing-format \'%ta \'")
      song += chr(3) + str(COLOR_DURATION)
      song += commands.getoutput("rhythmbox-client --print-playing-format \'(%te/%td)\'")
      xchat.command("say "+ song)
    if message == "!rhelp":
      xchat.command("say "+ chr(2) + chr(3) + str(COLOR_MESSAGES) + "!rstart starts rhythmbox, !rplay plays/pauses, !rshow lists the current song, !rvol+/- changes volume, !rvol <0-100>,!rhide hides the rhythmbox interface, !rquit stops rhythmbox")
    if not opped:
      return xchat.EAT_NONE
    if message == "!rstart":
      x = commands.getoutput("rhythmbox-client --play")
    if message == "!rplay":
      x = commands.getoutput("rhythmbox-client --play-pause")
    if message == "!rquit":
      x = commands.getoutput("rhythmbox-client --quit")
    if message == "!rhide":
      x = commands.getoutput("rhythmbox-client --hide")
    if message == "!rnext":
      x = commands.getoutput("rhythmbox-client --next")
    if message == "!rprev":
      x = commands.getoutput("rhythmbox-client --previous")
    if message == "!rvol+":
      x = commands.getoutput("rhythmbox-client --volume-up")
    if message == "!rvol-":
      x = commands.getoutput("rhythmbox-client --volume-down")
    if message.find("!rvol") == 0:
      newvol = word[1][5:]
      newvol = newvol.strip()
      if newvol.isdigit():
        newvol = float(newvol) / 100
      if newvol >= 0 and newvol <= 1:
        x = commands.getoutput("rhythmbox-client --set-volume "+ str(newvol))
    return xchat.EAT_NONE

xchat.hook_print("Channel Message", message_cb)
xchat.hook_print("Your Message", message_cb) 
